package com.biz.primus.ms.thirdlogin.api;

import com.biz.primus.base.enums.ChannelTypes;
import com.biz.primus.base.exception.BizBusinessException;
import com.biz.primus.common.utils.StringUtil;
import com.biz.primus.model.member.vo.MemberVo;
import com.biz.primus.model.thirdlogin.enums.OAuthTypes;
import com.biz.primus.model.thirdlogin.vo.OAuthMemberVo;
import com.biz.primus.ms.base.api.BaseApiController;
import com.biz.primus.ms.thirdlogin.feign.MemberFeignClient;
import com.biz.primus.ms.thirdlogin.service.ThirdloginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author jun.liu
 * @date 2017/9/29
 */
@RestController
@Api("第三方登录相关接口文档")
@RequestMapping("soa/thirdloginService/")
public class ThirdloginApiController extends BaseApiController
{

    private final ThirdloginService thirdloginService;
    private final MemberFeignClient memberFeignClient;

    @Autowired
    public ThirdloginApiController(ThirdloginService thirdloginService, @Qualifier(value = "memberFeignClient") MemberFeignClient memberFeignClient)
    {
        this.thirdloginService = thirdloginService;
        this.memberFeignClient = memberFeignClient;
    }

    //针对网站login 第三方登录
    //登录获取到第三方id 查询本地系统数据是否存在
    //存在无需再进行绑定直接走正常流程
    //不存在则需要绑定账号
    //将第三方登录的id和用户账号绑定的同时
    //需要进行本地系统会员查询（查询到没有 需要进行自动注册，查询到有，进行绑定）


    //针对移动login 第三方登录
    //接收移动客户端提交的第三方id查询本地系统
    //查询到有更新绑定信息
    //查询到没有 移动端进行第三方和本地会员进行绑定操作 将操作的数据传递到本地系统进行保存


    @ApiOperation("回掉查询本地系统关系[code来源于第三方登录回掉返回]")
    @GetMapping("callbackQueryByCode")
    public OAuthMemberVo callbackQueryByCode(@ApiParam(value = "第三方登录类型", required = true)
                                  @RequestParam("oAuthType") OAuthTypes oAuthType,
                                  @ApiParam(value = "第三方登录回掉返回的code", required = true)
                                  @RequestParam("code") String code)
    {
        return thirdloginService.queryOAuthMemberByOAuthId(oAuthType, queryOAuthIdByCode(oAuthType, code));
    }


    //如果查询本地系统关系绑定为null 证明本地关系未绑定 返回null给调用者 调用者监听到是null 则跳转到绑定页面进行绑定
    //如果查询本地系统关系绑定不为null 证明本地关系已绑定 返回查询到的对象给调用者 调用者监听到不是null 则跳转到正常逻辑流程
    @ApiOperation("根据类型和code查询vo对象[code来源于第三方登录回掉返回]")
    @GetMapping("queryOAuthMemberByOAuthId")
    public OAuthMemberVo queryOAuthMemberByOAuthId(@ApiParam(value = "第三方登录类型", required = true)
                                                   @RequestParam("oAuthType") OAuthTypes oAuthType,
                                                   @ApiParam(value = "oAuthId", required = true)
                                                   @RequestParam("oAuthId") String oAuthId)
    {
        return thirdloginService.queryOAuthMemberByOAuthId(oAuthType, oAuthId);
    }

    @ApiOperation("根据code查询第三方登录后的oAuthId\n code用于查询AccessToken\n 根据AccessToken查询oAuthId\n")
    @GetMapping("queryOAuthIdByCode")
    public String queryOAuthIdByCode(@ApiParam(value = "第三方登录类型", required = true)
                                     @RequestParam("oAuthType") OAuthTypes oAuthType,
                                     @ApiParam(value = "第三方登录回掉返回的code", required = true)
                                     @RequestParam("code") String code)
    {
        Optional<OAuthMemberVo> vo = Optional.ofNullable(thirdloginService.queryOAuthMemberByAccessToken(oAuthType, code));
        if(vo.isPresent())
        {
            return vo.get().getOAuthId();
        }
        throw new BizBusinessException("OAuthMemberVo query is null");
    }

    @ApiOperation("绑定第三方登录id和本地系统会员id关系")
    @PostMapping("bindingOnMobile")
    public OAuthMemberVo bindingOnMobile(@ApiParam(value = "第三方登录类型", required = true)
                                         @RequestParam("oAuthType") OAuthTypes oAuthType,
                                         @ApiParam(value = "登录渠道", required = true)
                                         @RequestParam("channelType") ChannelTypes channelType,
                                         @ApiParam(value = "oAuthId", required = true)
                                         @RequestParam("oAuthId") String oAuthId,
                                         @ApiParam(value = "手机号", required = true)
                                         @RequestParam("mobile") String mobile)
    {
        if(!StringUtil.isMobilePhoneNumber(mobile))
        {
            throw new BizBusinessException("mobile is not phonenumber");
        }
        Optional<MemberVo> mvo = Optional.ofNullable(memberFeignClient.getMemberByMobile(mobile));
        if(!mvo.isPresent())//不存在 则新建本地系统会员
        {
            MemberVo vo = new MemberVo();
            vo.setMobile(mobile);
            vo = memberFeignClient.bindingSaveMemberByMobile(vo);
            mvo = Optional.of(vo);
        }
        return thirdloginService.binding(oAuthType, channelType, oAuthId, mvo.get().getId());
    }

    @ApiOperation("解绑")
    @PostMapping("unBindingByMemberId")
    public void unBindingByMemberId(@ApiParam(value = "第三方登录类型", required = true)
                          @RequestParam("oAuthType") OAuthTypes oAuthType,
                          @ApiParam(value = "oAuthId", required = true)
                          @RequestParam("oAuthId") String oAuthId,
                          @ApiParam(value = "memberId", required = true)
                          @RequestParam("memberId") Long memberId)
    {
        thirdloginService.unBinding(oAuthType, oAuthId, memberId);
    }

    @ApiOperation("解绑")
    @PostMapping("unBindingByMobile")
    public void unBindingByMobile(@ApiParam(value = "第三方登录类型", required = true)
                          @RequestParam("oAuthType") OAuthTypes oAuthType,
                          @ApiParam(value = "oAuthId", required = true)
                          @RequestParam("oAuthId") String oAuthId,
                          @ApiParam(value = "手机号", required = true)
                          @RequestParam("mobile") String mobile)
    {
        if(!StringUtil.isMobilePhoneNumber(mobile))
        {
            throw new BizBusinessException("mobile is not phonenumber");
        }
        Optional<MemberVo> mvo = Optional.ofNullable(memberFeignClient.getMemberByMobile(mobile));
        if(mvo.isPresent())
        {
            thirdloginService.unBinding(oAuthType, oAuthId, mvo.get().getId());
        }
        else
        {
            thirdloginService.unBinding(oAuthType, oAuthId);
        }
    }

}


